/**
 * 百度地图经纬度转腾讯地图经纬度
 */
export function bMapTransQQMap(lng, lat) {
    let x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    let x = lng - 0.0065;
    let y = lat - 0.006;
    let z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
    let theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
    let lngs = z * Math.cos(theta);
    let lats = z * Math.sin(theta);
    return {
        lng: lngs,
        lat: lats
    }
}
/**
 * 腾讯地图经纬度转百度地图经纬度
 */
export function qqMapTransBMap(lng, lat) {
    let x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    let x = lng;
    let y = lat;
    let z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
    let theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
    let lngs = z * Math.cos(theta) + 0.0065;
    let lats = z * Math.sin(theta) + 0.006;
    return {
        lng: lngs,
        lat: lats
    }
}

/**
 * 调用高德获取定位信息
 */
export function locationInfo(callback = () => {}, key) {
    uni.getLocation({
        type: 'wgs84',
        geocode: true,
        success: position => {
            uni.request({
                url: `https://restapi.amap.com/v3/geocode/regeo`,
                method: 'GET',
                data: {
                    key: this.mapKey || key,
                    location: `${position.longitude},${position.latitude}`,
                    // extensions: extensions ? 'all' : 'base'
                },
                success: (res) => {
                    if (res.data.status == '1') {
                        let address = res.data.regeocode.addressComponent;
                        let locationInfo = {};
                        locationInfo.areaCode = address.adcode;
                        locationInfo.name = `${address.city} ${address.district}`
                        locationInfo.longitude = address.streetNumber.location.split(',')[0];
                        locationInfo.latitude = address.streetNumber.location.split(',')[1];
                        callback(locationInfo)
                    } else {
                        throw Error('获取定位信息失败!')
                    }

                }
            });
        },
        fail: err => {
            if (err.errMsg == 'getLocation:fail auth deny') {
                this.$toast('您拒绝了授权获取位置信息,如需获取请手动点击获取')
            }
        }
    });
}
